﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lchealthy
{
    public partial class frmininventoryshow : Form
    {
        DataSet ds;
        public frmininventoryshow()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            bind();
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddldate.Text == "之间")
            {
                dtps.Visible = true;
                dtpe.Visible = true;
            }
            else if (ddldate.Text == "所有")
            {
                dtps.Visible = false;
                dtpe.Visible = false;
            }
            else
            {
                dtps.Visible = true;
                dtpe.Visible = false;
            }
        }
        private void bind()
        {
            dgvin.AutoGenerateColumns = false;

            string sql;
            sql = "SELECT PName, PPrice, innum,pprice* innum as psum,inday FROM ProductInfo INNER JOIN ininventory ON ProductInfo.PID = ininventory.Pid where did=" + userinfo.id;
            if (ddldate.Text == "大于")
            {
                sql += string.Format(" and inday>#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "大于")
            {
                sql += string.Format(" and inday>#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "小于")
            {
                sql += string.Format(" and inday<#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "等于")
            {
                sql += string.Format(" and inday =#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "之间")
            {
                sql += string.Format(" and inday>#{0}# and inday<#{1}#", dtps.Value.ToShortDateString(), dtpe.Value.ToShortDateString());
            }
            sql += " order by inday desc";
            ds = DbHelperOleDb.Query(sql);
            decimal a = 0, s = 0;
            DataRow dr = ds.Tables[0].NewRow();
            dr[0] = "总计：";
            if (ds.Tables[0].Rows.Count > 0)
            {
                s = int.Parse(ds.Tables[0].Compute("sum(innum)", "").ToString());
                a = decimal.Parse(ds.Tables[0].Compute("sum(psum)", "").ToString());
            }
            //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            //{
            //    a += Convert.ToDecimal(ds.Tables[0].Rows[i][3]);
            //    s += Convert.ToDecimal(ds.Tables[0].Rows[i][2]);
            //}
            dr[3] = a;
            dr[2] = s;
            ds.Tables[0].Rows.Add(dr);
            dgvin.DataSource = ds.Tables[0];
            int widths = 0;
            for (int i = 0; i < dgvin.Columns.Count; i++)
            {
                dgvin.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCellsExceptHeader);  // 自动调整列宽  
                widths += dgvin.Columns[i].Width;   // 计算调整列后单元列的宽度和                       
            }
            if (widths >= dgvin.Size.Width)  // 如果调整列的宽度大于设定列宽  
                dgvin.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;  // 调整列的模式 自动  
            else
                dgvin.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;  // 如果小于 则填充  


        }

        private void frmininventoryshow_Load(object sender, EventArgs e)
        {
            ddldate.Text = "所有";
            dtps.Value = DateTime.Now;
            dtpe.Value = DateTime.Now;
            bind();
        }

        private void btnOut_Click(object sender, EventArgs e)
        {
            myfun.DataToExcel(dgvin);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            myprint m = new myprint(dgvin);
            m.Print();
        }
    }
}
